package cn.stylefeng.guns.modular.rest.storage;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicArea;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.entity.BasicTray;
import cn.stylefeng.guns.modular.basic.model.params.BasicAreaParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicGoodsParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicPositionParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicTrayParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicWarehouseParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicAreaResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicGoodsResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicPositionResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicTrayResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicWarehouseResult;
import cn.stylefeng.guns.modular.basic.service.BasicAreaService;
import cn.stylefeng.guns.modular.basic.service.BasicGoodsService;
import cn.stylefeng.guns.modular.basic.service.BasicPositionService;
import cn.stylefeng.guns.modular.basic.service.BasicTrayService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.rest.aop.UserAop;
import cn.stylefeng.guns.modular.storage.model.params.StorageStockParam;
import cn.stylefeng.guns.modular.storage.model.result.StorageStockResult;
import cn.stylefeng.guns.modular.storage.service.StorageOutDetailedService;
import cn.stylefeng.guns.modular.storage.service.StorageStockService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 库内
 */
@Controller
@RequestMapping("/rest/storage")
public class storageController extends BaseController {

    @Autowired
    private BasicTrayService basicTrayService;

    @Autowired
    private StorageStockService storageStockService;
    
    @Autowired
    private UserAop userAop;

    @Autowired
    private BasicGoodsService basicGoodsService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private BasicAreaService basicAreaService;

    @Autowired
    private BasicPositionService basicPositionService;

    @Autowired
    private StorageOutDetailedService storageOutDetailedService;

    /**
     * 查询仓库列表
     */
    @ResponseBody
    @RequestMapping("/Warehouselist")
    public ResponseData Warehouselist(BasicWarehouseParam basicWarehouseParam) {
    	User user = userAop.getUser();
    	basicWarehouseParam.setDeliverId(user.getDeliverId());
    	Long deptId = user.getDeptId();
        return ResponseData.success(this.basicWarehouseService.findListBySpec(basicWarehouseParam,deptId,user.getOperateId()));
    }

    /**
     * 查询库区列表
     */
    @ResponseBody
    @RequestMapping("/Arealist")
    public ResponseData Arealist(BasicAreaParam basicAreaParam ) {
    	User user = userAop.getUser();
    	basicAreaParam.setDeliverId(user.getDeliverId());
    	Long deptId = user.getDeptId();
        return ResponseData.success(this.basicAreaService.findListBySpec(basicAreaParam,deptId,user.getOperateId()));
    }

    /**
     * 查询库位列表
     */
    @ResponseBody
    @RequestMapping("/Positionlist")
    public ResponseData Positionlist(BasicPositionParam basicPositionParam ) {
    	User user = userAop.getUser();
    	basicPositionParam.setDeliverId(user.getDeliverId());
    	Long deptId = user.getDeptId();
        return ResponseData.success(this.basicPositionService.findListBySpec(basicPositionParam,null,deptId,null,user.getOperateId()));
    }

    /**
     * 查询仓库-库区-库位   三级联动
     */
    @ResponseBody
    @RequestMapping("/getWarehouselist")
    public ResponseData getWarehouselist() {
    	User user = userAop.getUser();
        storageOutDetailedService.unlockTheItemNew(user);
    	Long deliverId = user.getDeliverId();
    	Long deptId = user.getDeptId();
    	BasicWarehouseParam basicWarehouseParam = new BasicWarehouseParam();
    	basicWarehouseParam.setDeliverId(deliverId);
    	List<BasicWarehouseResult> wlist = this.basicWarehouseService.findListBySpec(basicWarehouseParam,deptId,user.getOperateId());
    	List<children> list = new ArrayList<children>();
    	for (BasicWarehouseResult w : wlist) {
    		BasicAreaParam basicAreaParam = new BasicAreaParam();
        	basicAreaParam.setDeliverId(deliverId);
        	basicAreaParam.setAreaWarehouseId(w.getId());
        	List<BasicAreaResult> alist = this.basicAreaService.findListBySpec(basicAreaParam,deptId,user.getOperateId());
        	List<children2> list2 = new ArrayList<children2>();
        	for (BasicAreaResult a : alist) {
        		BasicPositionParam basicPositionParam = new BasicPositionParam();
        		basicPositionParam.setPositionState("1");
            	basicPositionParam.setDeliverId(deliverId);
            	basicPositionParam.setPositionAreaId(a.getId());
            	List<BasicPositionResult> plist = this.basicPositionService.findListBySpec(basicPositionParam,null,deptId,null,user.getOperateId());
            	List<children3> list3 = new ArrayList<children3>();
            	for (BasicPositionResult p : plist) {
                	children3 children3 = new children3();
                	children3.setValue(p.getId());
                	children3.setLabel(p.getPositionNumber());
                	list3.add(children3);
				}
            	children2 children2 = new children2();
            	children2.setChildren(list3);
            	children2.setValue(a.getId());
            	children2.setLabel(a.getAreaName());
            	list2.add(children2);
			}
        	children children = new children();
        	children.setChildren(list2);
        	children.setValue(w.getId());
        	children.setLabel(w.getWarehouseName());
        	list.add(children);
		}
        return ResponseData.success(list);
    }


	/**
	 * 查询推荐库位列表
	 */
	@ResponseBody
	@RequestMapping("/getRecommendWarehouselist")
	public ResponseData getRecommendWarehouselist(@Param("warehouseId") Long warehouseId ,@Param("goodsNumber") String goodsNumber){

		List<Map<String,String>> wlist = this.basicWarehouseService.getRecommendWarehouselist(warehouseId,goodsNumber);

		return ResponseData.success(wlist);

	}
    /**
     * 查询仓库-库区-库位   三级联动
     */
    @ResponseBody
    @RequestMapping("/getWarehouselist2")
    public ResponseData getWarehouselist2() {
    	User user = userAop.getUser();
    	Long deliverId = user.getDeliverId();
    	Long deptId = user.getDeptId();
    	BasicWarehouseParam basicWarehouseParam = new BasicWarehouseParam();
    	basicWarehouseParam.setDeliverId(deliverId);
    	List<BasicWarehouseResult> wlist = this.basicWarehouseService.findListBySpec(basicWarehouseParam,deptId,user.getOperateId());
    	List<children> list = new ArrayList<children>();
    	for (BasicWarehouseResult w : wlist) {
    		BasicAreaParam basicAreaParam = new BasicAreaParam();
        	basicAreaParam.setDeliverId(deliverId);
        	basicAreaParam.setAreaWarehouseId(w.getId());
        	List<BasicAreaResult> alist = this.basicAreaService.findListBySpec(basicAreaParam,deptId,user.getOperateId());
        	List<children2> list2 = new ArrayList<children2>();
        	for (BasicAreaResult a : alist) {
        		BasicPositionParam basicPositionParam = new BasicPositionParam();
            	basicPositionParam.setDeliverId(deliverId);
            	basicPositionParam.setPositionAreaId(a.getId());
            	List<BasicPositionResult> plist = this.basicPositionService.findListBySpec(basicPositionParam,null,deptId,null,user.getOperateId());
            	List<children3> list3 = new ArrayList<children3>();
            	children3 children33 = new children3();
            	children33.setValue(0);
            	children33.setLabel("全部");
            	list3.add(children33);
            	for (BasicPositionResult p : plist) {
                	children3 children3 = new children3();
                	children3.setValue(p.getId());
                	children3.setLabel(p.getPositionNumber());
                	list3.add(children3);
				}
            	children2 children2 = new children2();
            	children2.setChildren(list3);
            	children2.setValue(a.getId());
            	children2.setLabel(a.getAreaName());
            	list2.add(children2);
			}
        	children children = new children();
        	children.setChildren(list2);
        	children.setValue(w.getId());
        	children.setLabel(w.getWarehouseName());
        	list.add(children);
		}
        return ResponseData.success(list);
    }

    /**
     * 查询商品pageList
     *
     */
    @ResponseBody
    @RequestMapping("/findgoodsPagelist")
    public LayuiPageInfo findgoodsPagelist(BasicGoodsParam basicGoodsParam,String condition ) {
    	User user = userAop.getUser();
        return this.basicGoodsService.findPageBySpec(basicGoodsParam,condition,user.getDeptId(),user.getOperateId());
    }

    /**
     * 查询商品
     *
     */
    @ResponseBody
    @RequestMapping("/goodslist")
    public ResponseData goodslist(BasicGoodsParam basicGoodsParam,String condition ) {
    	User user = userAop.getUser();
        basicGoodsParam.setDeliverId(user.getDeliverId());
        return ResponseData.success(this.basicGoodsService.findListBySpecNew(basicGoodsParam, user.getDeptId(),condition,user.getOperateId()));
    }

    /**
     * 修改商品接口
     */
    @RequestMapping("/editGoods")
    @ResponseBody
    public ResponseData editGoods(BasicGoodsParam basicGoodsParam) {
    	if(ToolUtil.isNotEmpty(basicGoodsParam.getGoodsNumber())) {
    		BasicGoodsParam param = new BasicGoodsParam();
    		param.setId(basicGoodsParam.getId());
    		param.setGoodsCustomerId(basicGoodsParam.getGoodsCustomerId());
    		param.setGoodsNumber(basicGoodsParam.getGoodsNumber());
    		BasicGoodsResult result = basicGoodsService.findBySpec(param,null);
    		if(result != null) {
    			return ResponseData.error("Product number already exists");
    		}
    	}
        
    	if(ToolUtil.isNotEmpty(basicGoodsParam.getGoodsBottom()) && ToolUtil.isNotEmpty(basicGoodsParam.getGoodsLamination())) {
    		BigDecimal GoodsBottom = new BigDecimal(basicGoodsParam.getGoodsBottom());
    		BigDecimal GoodsLamination = new BigDecimal(basicGoodsParam.getGoodsLamination());
    		BigDecimal ngoodsSupportvolume = GoodsBottom.multiply(GoodsLamination);
        	if(ToolUtil.isNotEmpty(basicGoodsParam.getXianggui())) {
        		BigDecimal xianggui = new BigDecimal(basicGoodsParam.getXianggui());
        		BigDecimal ngoodsSupportvolume2 = ngoodsSupportvolume.multiply(xianggui);
        		basicGoodsParam.setGoodsSupportvolume(ngoodsSupportvolume2.toString());
        	} else {
        		basicGoodsParam.setGoodsSupportvolume(ngoodsSupportvolume.toString());
        	}
    	}
        this.basicGoodsService.update(basicGoodsParam);
        return ResponseData.success();
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicGoodsParam basicGoodsParam) {
    	User user = userAop.getUser();
        if(basicGoodsParam!=null){
        	String goodsName = basicGoodsParam.getGoodsName();
        	//解决参数中的 英文括号
        	if (goodsName!=null&&goodsName!="") {
        		goodsName = goodsName.replace("& #40;", "(");
        		goodsName = goodsName.replace("& #41;", ")");
            	basicGoodsParam.setGoodsName(goodsName);
        	}
        }
        
        basicGoodsParam.setDeliverId(user.getDeliverId());
        basicGoodsParam.setDeptId(user.getDeptId());
        
        BasicGoodsParam param = new BasicGoodsParam();
        param.setGoodsCustomerId(basicGoodsParam.getGoodsCustomerId());
        param.setGoodsNumber(basicGoodsParam.getGoodsNumber());
        BasicGoodsResult result = basicGoodsService.findBySpec(param,null);
        if(result != null) {
        	return ResponseData.error("Product number already exists");
        }

    	if(ToolUtil.isNotEmpty(basicGoodsParam.getGoodsBottom()) && ToolUtil.isNotEmpty(basicGoodsParam.getGoodsLamination())) {
    		BigDecimal GoodsBottom = new BigDecimal(basicGoodsParam.getGoodsBottom());
    		BigDecimal GoodsLamination = new BigDecimal(basicGoodsParam.getGoodsLamination());
    		BigDecimal ngoodsSupportvolume = GoodsBottom.multiply(GoodsLamination);
        	if(ToolUtil.isNotEmpty(basicGoodsParam.getXianggui())) {
        		BigDecimal xianggui = new BigDecimal(basicGoodsParam.getXianggui());
        		BigDecimal ngoodsSupportvolume2 = ngoodsSupportvolume.multiply(xianggui);
        		basicGoodsParam.setGoodsSupportvolume(ngoodsSupportvolume2.toString());
        	} else {
        		basicGoodsParam.setGoodsSupportvolume(ngoodsSupportvolume.toString());
        	}
    	}
    	
    	if(ToolUtil.isEmpty(basicGoodsParam.getGoodsTraylayer())) {
    		basicGoodsParam.setGoodsTraylayer("1");
    	} 
    	
    	if(ToolUtil.isEmpty(basicGoodsParam.getGoodsVolume())) {
    		basicGoodsParam.setGoodsVolume("0.1");
    	} 
    	
    	if(ToolUtil.isEmpty(basicGoodsParam.getGoodsWeight())) {
    		basicGoodsParam.setGoodsWeight("0.1");
    	} 
    	
    	if(ToolUtil.isEmpty(basicGoodsParam.getGoodsCharging())) {
    		basicGoodsParam.setGoodsCharging("10");
    	} 
    	
    	if(ToolUtil.isEmpty(basicGoodsParam.getGoodsWarranty())) {
    		basicGoodsParam.setGoodsWarranty("36");
    	} 
    	
        this.basicGoodsService.add(basicGoodsParam);
        return ResponseData.success();
    }

	@Autowired
    private DeptService deptService;
	
	/**
	 * 获取所属项目单号前缀
	 * @param user
	 * @return
	 */
	public String getOperateIdOrderPrefix(User user) {
		if(user.getOperateId() != null) {
			Dept dept = new Dept();
			dept.setOperateId(user.getOperateId());
			Dept resdept = deptService.findBySpec(dept);
			if(resdept != null) {
				if(ToolUtil.isNotEmpty(resdept.getOrderPrefix())) {
					return resdept.getOrderPrefix();
				} else {
					return user.getAccount().substring(0, 3);
				}
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

    /**
     * 添加库存
     */
    @RequestMapping(path = "/addStorage" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public ResponseData addStorage(StorageStockParam storageStockParam) {
    	User user = userAop.getUser();
            //托盘信息
        BasicTrayParam Tp = new BasicTrayParam();
        BasicTray basicTray = basicTrayService.findByThelastone();
        Tp.setTrayState("occupied");//托盘状态
        int TrayNumber = 0;
		String trayCode = getOperateIdOrderPrefix(user)+"-";
        if (basicTray!=null) {
            TrayNumber = (basicTray.getId()+1);
        } else {
            TrayNumber = 1;
        }
        StorageStockParam stockParam = new StorageStockParam();
		BasicTrayParam trayParam = new BasicTrayParam();
		trayParam.setDeliverId(user.getDeliverId());
		trayParam.setDeptId(user.getDeptId());
		trayParam.setTrayState("idle");
		BasicTrayResult basicTrayResult = basicTrayService.findBySpec(trayParam,user.getOperateId());
		if (basicTrayResult != null) {
			stockParam.setTrayNumber(basicTrayResult.getTrayNumber());
			stockParam.setTrayId(Long.parseLong(basicTrayResult.getId().toString()));
			basicTrayResult.setTrayState("occupied");
			ToolUtil.copyProperties(basicTrayResult, trayParam);
			basicTrayService.update(trayParam);
		} else {
			Tp.setTrayNumber(trayCode+TrayNumber);
			Tp.setDeliverId(user.getDeliverId());
			Tp.setDeptId(user.getDeptId());
			basicTrayService.add(Tp);
			stockParam.setTrayNumber(trayCode+TrayNumber);//托盘编号
			BasicTrayResult basicTrayResult2 = basicTrayService.findBySpec(Tp,user.getOperateId());
			stockParam.setTrayId(Long.parseLong(basicTrayResult2.getId().toString()));
		}
        
        stockParam.setCustomerName(storageStockParam.getCustomerName());
        stockParam.setCustomerId(storageStockParam.getCustomerId());
        
        BasicGoodsParam basicGoodsParam = new BasicGoodsParam();
        basicGoodsParam.setGoodsNumber(storageStockParam.getGoodsNumber());
        basicGoodsParam.setDeliverId(user.getDeliverId());
        basicGoodsParam.setGoodsCustomerId(Integer.parseInt(storageStockParam.getCustomerId().toString()));
        BasicGoodsResult basicGoodsResult = basicGoodsService.findBySpec(basicGoodsParam,null);
		BasicArea basicArea = basicAreaService.getById(storageStockParam.getAreaId());
        stockParam.setGoodsId(Long.valueOf(basicGoodsResult.getId()));
        
        stockParam.setTrayId((long) TrayNumber);//托盘ID
        stockParam.setStockstate("in_stock");//状态
        stockParam.setGoodsbatchs(storageStockParam.getProducetime().replace("-",""));//批次
        stockParam.setPutTime(storageStockParam.getPutTime());
        
		if (ToolUtil.isNotEmpty(basicGoodsResult.getGoodsWeight())) {
			BigDecimal w = new BigDecimal(basicGoodsResult.getGoodsWeight());
			BigDecimal n = new BigDecimal(storageStockParam.getNum());
	        stockParam.setStockWeight(w.multiply(n).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//重量
		} else {
	        stockParam.setStockWeight("1");//重量
		}
		
		if (ToolUtil.isNotEmpty(basicGoodsResult.getGoodsVolume())) {
			BigDecimal gv = new BigDecimal(basicGoodsResult.getGoodsVolume());
			BigDecimal n = new BigDecimal(storageStockParam.getNum());
			stockParam.setStockVolume(gv.multiply(n).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//体积
		} else {
			stockParam.setStockVolume("1");//体积
		}
		
		if (ToolUtil.isNotEmpty(basicGoodsResult.getGoodsCharging())) {
			BigDecimal p = new BigDecimal(basicGoodsResult.getGoodsCharging());
			BigDecimal n = new BigDecimal(storageStockParam.getNum());
			stockParam.setStockMoney(p.multiply(n).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//库存金额
	        stockParam.setUnitprice(basicGoodsResult.getGoodsCharging());//单价
		} else {
	        stockParam.setUnitprice("1");//单价
	        stockParam.setStockMoney(storageStockParam.getNum());//库存金额
		}
		
        stockParam.setProducetime(storageStockParam.getProducetime());//生产日期
        if(ToolUtil.isEmpty(storageStockParam.getGoodsbatchs())) {
        	storageStockParam.setGoodsbatchs(storageStockParam.getProducetime().replaceAll("-", ""));
        }
        
        stockParam.setOperateId(storageStockParam.getOperateId());
        stockParam.setOperateName(storageStockParam.getOperateName());
        
        stockParam.setWarehouseName(storageStockParam.getWarehouseName());
        stockParam.setWarehouseId(storageStockParam.getWarehouseId());
        
        stockParam.setAreaId(storageStockParam.getAreaId());
        stockParam.setAreaNumber(basicArea.getAreaNumber());
        
        stockParam.setPositionNumber(storageStockParam.getPositionNumber());
        stockParam.setPositionId(storageStockParam.getPositionId());
        
        stockParam.setNum(storageStockParam.getNum());
        stockParam.setBalenum(storageStockParam.getNum());
        stockParam.setCanNum(storageStockParam.getNum());
        stockParam.setGoodsName(storageStockParam.getGoodsName());
        stockParam.setGoodsNumber(storageStockParam.getGoodsNumber());
        
        stockParam.setDeliverId(user.getDeliverId());
        stockParam.setDeptId(user.getDeptId());
        this.storageStockService.add(stockParam);
        return ResponseData.success();
    }

    /**
     * 库存查询
     * （页面一）1查询商品总量，体积总量
     */
    @RequestMapping("/findTotalBySpec")
    @ResponseBody
    public ResponseData findTotalBySpec(StorageStockParam storageStockParam,String pNumber) {
    	User user = userAop.getUser();
        storageOutDetailedService.unlockTheItemNew(user);
    	if(ToolUtil.isNotEmpty(pNumber)) {
    		BasicPositionParam basicPositionParam = new BasicPositionParam();
    		basicPositionParam.setPositionNumber(pNumber);
    		List<BasicPositionResult> list = basicPositionService.findListBySpec(basicPositionParam, null, user.getDeptId(), 0l,user.getOperateId());
    		if(list.size()==0) {
    			return ResponseData.error("库位不存在！");
    		} else if(list.size()==1) {
    			storageStockParam.setPositionId(Long.parseLong(list.get(0).getId().toString()));
    		} else {
    			for (BasicPositionResult r : list) {
        			storageStockParam.setPositionId(Long.parseLong(r.getId().toString()));
    		    	if (storageStockParam.getWarehouseId() == null) {
    		    		if (storageStockParam.getAreaId() == null && storageStockParam.getPositionId() == null && storageStockParam.getGoodsId() == null) {
    		    			BasicWarehouseParam param = new BasicWarehouseParam();
    		    			BasicWarehouseResult result = basicWarehouseService.findBySpec(param , user.getDeptId(),user.getOperateId() );
    		    			storageStockParam.setWarehouseId(Long.parseLong(result.getId().toString()));
    					}
    				}
    		    	storageStockParam.setDeliverId(user.getDeliverId());
    		    	storageStockParam.setDeptId(user.getDeptId());
    		    	storageStockParam.setStockstate("在库");
    		    	StorageStockResult ssr = storageStockService.findTotalBySpec(storageStockParam,user.getOperateId());
    		    	StorageStockResult ssr2 = storageStockService.findTotalgoodsTypeBySpec(storageStockParam,user.getOperateId());//查询品类总量
    		    	if (ssr2!=null&&ssr!=null) {
    		            if (!"0".equals(ssr2.getTotalgoodsType())) {
    		        		ssr.setTotalgoodsType(ssr2.getTotalgoodsType());
    					}
        		        return ResponseData.success(ssr);
    				}
				}
    			return ResponseData.success();
    		}
    	}
    	if (storageStockParam.getWarehouseId() == null) {
    		if (storageStockParam.getAreaId() == null && storageStockParam.getPositionId() == null && storageStockParam.getGoodsId() == null) {
    			BasicWarehouseParam param = new BasicWarehouseParam();
    			BasicWarehouseResult result = basicWarehouseService.findBySpec(param , user.getDeptId(),user.getOperateId());
    			storageStockParam.setWarehouseId(Long.parseLong(result.getId().toString()));
			}
		}
    	storageStockParam.setDeliverId(user.getDeliverId());
    	storageStockParam.setDeptId(user.getDeptId());
    	storageStockParam.setStockstate("在库");
    	StorageStockResult ssr = storageStockService.findTotalBySpec(storageStockParam,user.getOperateId());
    	StorageStockResult ssr2 = storageStockService.findTotalgoodsTypeBySpec(storageStockParam,user.getOperateId());//查询品类总量
    	if (ssr2!=null&&ssr!=null) {
            if (!"0".equals(ssr2.getTotalgoodsType())) {
        		ssr.setTotalgoodsType(ssr2.getTotalgoodsType());
			}
		}
        return ResponseData.success(ssr);
    }
    
    /**
     * 库存查询
     * （页面二）1查询商品总量，体积总量
     */
    @RequestMapping("/findGoodsListBySpec")
    @ResponseBody
    public ResponseData findGoodsListBySpec(StorageStockParam storageStockParam,String pNumber) {
    	User user = userAop.getUser();
    	if(ToolUtil.isNotEmpty(pNumber)) {
    		BasicPositionParam basicPositionParam = new BasicPositionParam();
    		basicPositionParam.setPositionNumber(pNumber);
    		List<BasicPositionResult> list = basicPositionService.findListBySpec(basicPositionParam, null, user.getDeptId(), 0l,user.getOperateId());
    		if(list.size()==0) {
    			return ResponseData.error("库位不存在！");
    		} else if (list.size()==1) {
    			storageStockParam.setPositionId(Long.parseLong(list.get(0).getId().toString()));
    		} else {
    			for (BasicPositionResult r : list) {
        			storageStockParam.setPositionId(Long.parseLong(r.getId().toString()));
        	    	storageStockParam.setDeptId(user.getDeptId());
        	    	storageStockParam.setStockstate("在库");
        	    	List<StorageStockResult> slist = storageStockService.findGoodsListBySpecNew(storageStockParam,user.getOperateId());
        	    	if(slist.size()>0) {
        	    		return ResponseData.success(slist);
        	    	}
				}
    			return ResponseData.success();
    		}
    	}
    	storageStockParam.setDeptId(user.getDeptId());
    	storageStockParam.setStockstate("在库");
    	return ResponseData.success(storageStockService.findGoodsListBySpecNew(storageStockParam,user.getOperateId()));
    }

    /**
     * 库存查询
     * 查询批次总量，体积总量
     */
    @RequestMapping("/findPiciTotalBySpec")
    @ResponseBody
    public ResponseData findPiciTotalBySpec(StorageStockParam storageStockParam) {
    	User user = userAop.getUser();
        storageOutDetailedService.unlockTheItemNew(user);
    	storageStockParam.setDeliverId(user.getDeliverId());
    	storageStockParam.setDeptId(user.getDeptId());
    	storageStockParam.setStockstate("在库");
    	StorageStockResult ssr = storageStockService.findTotalBySpec(storageStockParam,user.getOperateId());
    	StorageStockResult ssr2 = storageStockService.findTotalgoodsTypeBySpec(storageStockParam,user.getOperateId());//查询品类总量
    	if (ssr2!=null&&ssr!=null) {
            if (!"0".equals(ssr2.getTotalgoodsType())) {
        		ssr.setTotalgoodsType(ssr2.getTotalgoodsType());
			}
		}
        return ResponseData.success(ssr);
    }

    /**
     * 库存查询
     * （页面一）2查询商品占比,前三和其它总和
     */
    @RequestMapping("/findGoodsProportion")
    @ResponseBody
    public ResponseData findGoodsProportion(StorageStockParam storageStockParam) {
    	User user = userAop.getUser();
    	storageStockParam.setDeliverId(user.getDeliverId());
    	storageStockParam.setDeptId(user.getDeptId());
    	storageStockParam.setStockstate("在库");
    	StorageStockResult ssr = storageStockService.findTotalBySpec(storageStockParam,user.getOperateId());//查询出仓库商品总量
    	List<StorageStockResult> ssrlist = storageStockService.findGoodsProportionNew(storageStockParam,user.getOperateId());//查询出仓库商品数量前4
        if (ssr!=null) {
        	BigDecimal totalNum = new BigDecimal(ssr.getTotalNum());//总量
        	BigDecimal ThreesumNum = new BigDecimal(0);//前三总量
        	for (StorageStockResult ssr3 : ssrlist) {
            	BigDecimal num = new BigDecimal(ssr3.getNum());
            	BigDecimal num2 = ThreesumNum.add(num);
            	ThreesumNum = num2;
    		}
        	BigDecimal other =  totalNum.subtract(ThreesumNum);//其它商品 总量
        	BigDecimal as = new BigDecimal(0);//前三总量
        	if (other.compareTo(as)==1) {
            	StorageStockResult nssr = new StorageStockResult();
            	nssr.setGoodsName("其它");
            	nssr.setNum(other.toString());
            	ssrlist.add(nssr);
			}
		}
        return ResponseData.success(ssrlist);
    }
    
    /**
     * 库存查询
     * （页面二）1查询商品总量，体积总量  点击详情
     */
    @RequestMapping("/findGoodsListBySpecms")
    @ResponseBody
    public ResponseData findGoodsListBySpecms(StorageStockParam storageStockParam) {
    	User user = userAop.getUser();
    	storageStockParam.setDeliverId(user.getDeliverId());
    	storageStockParam.setDeptId(user.getDeptId());
    	storageStockParam.setStockstate("在库");
    	return ResponseData.success(storageStockService.findGoodsListBySpecmsNew(storageStockParam,user.getOperateId()));
    }

    /**
     * 库存查询
     * （页面一）1查询商品总量，体积总量 (点击详情)
     */
    @RequestMapping("/findTotalBySpecms")
    @ResponseBody
    public ResponseData findTotalBySpecms(StorageStockParam storageStockParam) {
    	User user = userAop.getUser();
    	storageStockParam.setDeliverId(user.getDeliverId());
    	storageStockParam.setDeptId(user.getDeptId());
    	storageStockParam.setStockstate("在库");
    	StorageStockResult ssr = storageStockService.findTotalBySpecms(storageStockParam,user.getOperateId());
        return ResponseData.success(ssr);
    }
}
